<?php


namespace App\Models;


use App\Engine\BaseModel;

class Goods extends BaseModel
{
    protected $table = 'goods';
    public $timestamps = false;

    protected $fillable = [
        'name',
        'banners',
        'image',
        'price',
        'coupon_price',
        'shop_name',
        'stock',
        'address',
        'lat',
        'lng',
        'content',
        'code',
        'phone'
    ];

    public $rule=[
        'name'=>'required',
        'banners'=>'required',
        'image'=>'required',
        'price'=>'required',
        'coupon_price'=>'required',
        'shop_name'=>'required',
        'stock'=>'required',
        'address'=>'required',
        'lat'=>'required',
        'lng'=>'required',
        'content'=>'required',
        'phone'=>'required'

    ];
    public $ruleMessage=[
        'name.required'=>'名称不能为空',
        'banners.required'=>'请上传轮播图',
        'image.required'=>'请选择封面',
        'price.required'=>'请填写套餐价格',
        'coupon_price.required'=>'请输入兑换价格',
        'shop_name.required'=>'请输入商家名称',
        'stock.required'=>'请输入库存',
        'address.required'=>'请填写商家地址',
        'lat.required'=>'请选择位置',
        'lng.required'=>'请选择位置',
        'content.required'=>'请输入详情',
        'phone.required'=>"请输入电话"
    ];

    public function setBannersAttribute($value)
    {
        if ($value==""){
            $this->attributes["banners"]=json_encode([],256);
        }
        else{
            $this->attributes["banners"]=json_encode($value,256);
        }
    }

    public function getBannersAttribute($value)
    {
        if (!$value)
            return [];
        return json_decode($value,256);
    }
}
